#! /usr/bin/perl

#enable strict mode
use strict;

#use the html subs package adn the DBQueries package
use HTMLSubs;
use DBQueries;
use CGI;


#the username of the individual whose profiel shoudl be shown will be passed to this script via get by teh script callign it
#start by obtaining the username
my $input = obtain_input();

#ensure that input is not blank which would mean that the site is illegally accessed
if(length($input) == 0){
	
	#if so, redirect to login
	print CGI -> redirect("login.pl.cgi");
}

#now call the subroutine to decompoe the input into a hash of keys and values
my $ref_input_values = HTMLSubs -> decode_input($input);

#dereference
my %input_values = %$ref_input_values;

#declare username and group name
my $username = $input_values{'username'};
my $group_name = $input_values{'groupName'};

#we know that this script can only be accessed from teh delete button there are no different contexts in which this script will be called
#therefore we do not need to determine what actions to take and can delete the group
DBQueries::removeGroup($group_name);

#now proceed to show success message
#call method to display html beginnign
HTMLSubs->generate_html_beginning("Successfully Deleted $group_name");
	
#obtain general user info from DB
#declare scalars to hold most important information
my $groups_owned = scalar(DBQueries::getUserCreatedGroups($username));
my $groups_member = scalar(DBQueries::getUserGroups($username));
my $files_owned = scalar(DBQueries::getUserFileNames($username));
my $files_seeable = scalar(DBQueries::getAllUserFileNames($username));
my($first_name, $last_name) = DBQueries::getUserInfo($username);
	
#call sub to print header
HTMLSubs -> generate_display_head("Document Management System", "Successfully Deleted $group_name", $username);

print <<END_HTML;
	
<!--now display the message to the user-->
<h4>Please return to your profile for further action.</h4>
<br>
	
END_HTML

#create hash for button
my %username_hash;
$username_hash{'username'} = $username;
	
#call subroutine to display button that returns the user to the profile
HTMLSubs -> generate_html_button("profile.pl.cgi", "Go To profile", \%username_hash);

#now call method to produce closing html tags
HTMLSubs->generate_html_end();

#subroutine to obtain and determine what information is passed to the script
#to make this more universal, both GET and POST methods are accepted 
#this reads input for both GET and POST
sub obtain_input{

	#declare scalar to hold passed information
	my $input;

	#now retrieve the input through either get or post
	#change the case of request method
	$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

	#if post use STDIN
	if ($ENV{'REQUEST_METHOD'} eq "POST"){
		read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

		#exchange plus signs to spaces
		$input =~ s/\+/ /;

		#deal with special characters using the substitution sample provided
		$input =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
	}
	#otherwise the env hash
	else{
		$input = $ENV{'QUERY_STRING'};
		
		#exchange plus signs to spaces
		$input =~ s/\+/ /;
		
		#deal with special characters using the substitution sample provided
		$input =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
	}
	
	#now return the correct input
	return $input;	
}